package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.config.LoginInterceptor;
import com.prd.constant.IsStatus;
import com.prd.mapper.EntTaskCoachMapper;
import com.prd.model.EntTaskCoach;
import com.prd.model.LoginUser;
import com.prd.service.EntTaskCoachService;
import com.prd.service.UserService;
import com.prd.util.UserInfoUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EntTaskCoachServiceImpl extends ServiceImpl<EntTaskCoachMapper, EntTaskCoach> implements EntTaskCoachService {

    @Autowired
    private EntTaskCoachMapper entTaskCoachMapper;

    @Autowired
    private UserService userService;

    @Override
    public int insertInfo(EntTaskCoach entTaskCoach) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), true), entTaskCoach);
        entTaskCoach.setVerify(IsStatus.TWO.getCode());
        return entTaskCoachMapper.insert(entTaskCoach);
    }

    @Override
    public int updateInfo(EntTaskCoach entTaskCoach) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), false), entTaskCoach);
        return entTaskCoachMapper.updateById(entTaskCoach);
    }

    @Override
    public List<EntTaskCoach> listInfo(EntTaskCoach entTaskCoach) {
        LambdaQueryWrapper<EntTaskCoach> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.setEntity(entTaskCoach);
        List<EntTaskCoach> list = entTaskCoachMapper.selectList(queryWrapper);
        for (EntTaskCoach entTaskCoach1 : list) {
            entTaskCoach1.setSigner(userService.listName(Long.parseLong(entTaskCoach1.getSigner())).getMerge());
            entTaskCoach1.setRecipient(userService.listName(Long.parseLong(entTaskCoach1.getRecipient())).getMerge());
            entTaskCoach1.setCompile(userService.listName(Long.parseLong(entTaskCoach1.getCompile())).getMerge());
            entTaskCoach1.setMake(userService.listName(Long.parseLong(entTaskCoach1.getMake())).getMerge());
        }
        return list;
    }

    @Override
    public EntTaskCoach details(Long id) {
        return entTaskCoachMapper.selectById(id);
    }
}
